﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;
using System.Drawing;
using System.Collections.Specialized;
using popLib;

namespace PhotoMgr.src.common
{

    public class ExMultiSelectorLabel : Label
    {
        private ContextMenuStrip menu;
        private ExMultiSelectorLabelItem[] datasource;
        private string[] datasourcestring;

        public ExMultiSelectorLabel()
        {
            this.Font = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Underline);
            this.Cursor = Cursors.Hand;
            this.Click += new EventHandler(ExMultiSelectorLabel_Click);
            this.TextAlign = ContentAlignment.MiddleRight;

            datasource = new ExMultiSelectorLabelItem[0];
            datasourcestring = new string[0];
            menu = new ContextMenuStrip();

        }

        void ExMultiSelectorLabel_Click(object sender, EventArgs e)
        {
            if (menu.Items.Count > 1)
                menu.Show(this, new Point(0, this.Height));
        }



        public ExMultiSelectorLabelItem[] DataSource
        {
            get
            {
                return datasource;
            }

            set
            {
                datasource = value;
                updateMenuItem();
            }
        }

        /// <summary>
        /// string array string[]{ "a", "b", "c" } -> value is item index
        /// string array string[]{ "a=001", "b:002", "c|003" } -> value is after = : |
        /// </summary>
        public string[] DataSourceString
        {
            get { return datasourcestring; }
            set
            {
                datasourcestring = value;
                List<ExMultiSelectorLabelItem> list = new List<ExMultiSelectorLabelItem>();

                string[] parts;
                foreach (var item in datasourcestring)
                {
                    parts = item.SplitByAny("|:=");
                    list.Add(new ExMultiSelectorLabelItem() { Text = parts[0], Value = parts.Length > 1 ? parts[1] : menu.Items.Count.ToString() });
                }

                DataSource = list.ToArray();
            }
        }


        public string SelectedValue { get; set; }
        public string SelectedText { get; set; }

        void menuitem_Click(object sender, EventArgs e)
        {
            SelectedValue = ((ToolStripMenuItem)sender).Tag.ToString();
            SelectedText = ((ToolStripMenuItem)sender).Text;
            this.Text = SelectedText;
        }

        void updateMenuItem()
        {
            menu.Items.Clear();

            foreach (var item in DataSource)
            {
                var menuitem = new ToolStripMenuItem()
                {
                    Text = item.Text,
                    Tag = item.Value
                };
                menuitem.Click += new EventHandler(menuitem_Click);
                menu.Items.Add(menuitem);
            }

            if (menu.Items.Count > 0)
            {
                menuitem_Click(menu.Items[0], new EventArgs());

                if (menu.Items.Count == 1)
                {
                    this.Font = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Regular);
                    this.Cursor = Cursors.Default;
                }
                else
                {
                    this.Font = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Underline);
                    this.Cursor = Cursors.Hand;
                }
            }
            else
            {
                this.Text = "";
            }
        }
    }

    public class ExMultiSelectorLabelItem
    {
        public ExMultiSelectorLabelItem() { }
        public string Text { get; set; }
        public string Value { get; set; }
    }
}
